package com.excel;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.io.FileOutputStream;
import java.io.OutputStream;

public class ExcelUtils {

    public static void main(String[] args) throws Exception {
        createTableSheet();
    }

    public static void createTableSheet() throws Exception {

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("第一页");
        sheet.setColumnWidth(0, 70);

        HSSFCellStyle setBorder = workbook.createCellStyle();
        setBorder.setBorderBottom(BorderStyle.THIN); //下边框
        setBorder.setBorderLeft(BorderStyle.THIN);//左边框
        setBorder.setBorderTop(BorderStyle.THIN);//上边框
        setBorder.setBorderRight(BorderStyle.THIN);//右边框
        HSSFFont font = workbook.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 16);//设置字体大小

        setBorder.setFont(font);//选择需要用到的字体格式


        for (int n = 1; n <= 3; n++) {
            Row row = sheet.createRow(n);
            for (int i = 0; i < 10; i++) {
                Cell cell = row.createCell(2 + i);
                cell.setCellValue("" + n + "_" + i);
                cell.setCellStyle(setBorder);
            }
        }


        try (OutputStream fileOut = new FileOutputStream("D:\\Users\\yuxj001\\Desktop\\tmp\\tables.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (Exception e) {

        }
    }
}
